package com.seafwg.store.controller;

import com.seafwg.store.entity.User;
import com.seafwg.store.service.IUserService;
import com.seafwg.store.service.ex.InsertException;
import com.seafwg.store.service.ex.UsernameDuplicateException;
import com.seafwg.store.utils.ResultJson;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


/**
 * @Author seafwg
 * @Date 2022/3/25 4:11 下午
 * @Version 1.0
 * @description xxx
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Resource
    private IUserService userService;

    /**
     * 用户注册，通过统一处理异常信息的方法
     * @param user
     * @return
     */
    @RequestMapping("/regs")
    public ResultJson<Void> regUser(User user) {
        userService.register(user);
        return new ResultJson<>(OK);
    }

    /**
     * 用户注册，控制层处理异常信息的方法
     * @param user
     * @return
     */
    @RequestMapping("/reg")
    public ResultJson<Void> userRegister(User user) {
        ResultJson<Void> result = new ResultJson<>();
        // userService.register(user);
        try {
            userService.register(user);
            result.setStatus(200);
        } catch (UsernameDuplicateException e) {
            result.setStatus(4000);
            result.setMessage("用户名已存在");
        } catch (InsertException e) {
            result.setStatus(5000);
            result.setMessage("注册失败，请联系管理员");
        }
        return result;
    }

    /**
     * 用户登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public ResultJson<User> userLogin(User user, HttpSession session) {
        System.out.println("user:" + user);
        User loginUser = userService.login(user);

        // 登录成功后将uid和username保存到session中
        session.setAttribute("uid", loginUser.getUid());
        session.setAttribute("username", loginUser.getUsername());

        System.out.println("loginUser:"+ loginUser);
        return new ResultJson<>(OK, loginUser);
    }

    /**
     * 修改用户密码
     * @param oldPassword
     * @param newPassword
     * @param session
     * @return
     */
    @RequestMapping("/change_pwd")
    public ResultJson<Void> changePwd(String oldPassword, String newPassword, HttpSession session) {
        // 从session中获取当前用户的uid,username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFormSession(session);
        // 调用业务层修改密码
        userService.changePassword(uid, username, oldPassword, newPassword);
        return new ResultJson<>(OK);
    }

    /**
     * 根据用户uid获取用户信息
     * @param session
     * @return
     */
    @RequestMapping(value = "/get_user_uid", method = RequestMethod.GET)
    public ResultJson<User> getUserByUid(HttpSession session) {
        Integer uid = getUidFromSession(session);
        User user = userService.findUserByUid(uid);
        return new ResultJson<>(OK, user);
    }

    /**
     * 修改用户信息
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/change_user_info")
    public ResultJson<Void> changUserInfo(User user, HttpSession session) {
        // 获取用户数据
        Integer uid = getUidFromSession(session);
        String username = getUsernameFormSession(session);
        // 调用业务层方法
        userService.changeUserInfo(uid, username, user);
        return new ResultJson<>(OK);
    }

    /**
     * 修改用户头像
     * @param file
     * @param session
     * @return
     */
    @RequestMapping("/update_avatar")
    public ResultJson<String> changAvatar(MultipartFile file, HttpSession session) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFormSession(session);
        // 获取磁盘路径
        // session.getServletContext().getRealPath()
        String avatar = userService.changeAvatar(uid, username, session, file);
        return new ResultJson<>(OK, avatar);
    }
}
